﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TipokHostContract;
using TipokHostManager.Model;
using TipokHostManager.WMIManager;

namespace TipokHostManager
{
    public class HostServiceStrategy : IContractServiceStrategy
    {
        private Configuration.LocalSettings localSettings = new Configuration.LocalSettings();


        public List<ServiceModel> GetServices()
        {
            return ServiceManager.Instance.GetAllServices();
        }

        public ServiceModel GetService(int id, int serviceType)
        {
            return ServiceManager.Instance.GetService(GetProcessServiceName(null, serviceType));
        }

        public void InstallService(int id, int serviceType, string password)
        {
            string workPath = GetWorkerPath(null, serviceType);
            EnumServiceType STYPE = (EnumServiceType)serviceType;
            switch (STYPE)
            {
                case EnumServiceType.RC:
                    MoveDirectoryFile(localSettings.CSMasterDirectory, workPath);
                    break;
                case EnumServiceType.RTWTS:
                    MoveDirectoryFile(localSettings.RTWTSMasterDirectory, workPath);
                    break;
                default:
                    break;
            }
            BuildNewConfig(serviceType, null);
            if (Directory.Exists(workPath))
            {
                ServiceManager.Instance.InstallService(workPath);
            }
        }

        public void UnInstallService(int id, int serviceType)
        {
            string workPath = GetWorkerPath(null, serviceType);
            ServiceManager.Instance.UninstallService(workPath);
        }

        public void StartService(int id, int serviceType)
        {
            ServiceManager.Instance.StartService(GetProcessServiceName(null, serviceType));
        }

        public void StopService(int id, int serviceType)
        {
            ServiceManager.Instance.StopService(GetProcessServiceName(null, serviceType));
        }


        public void KillProcessForcibly(int id, int serviceType)
        {
        }
        public void RestartService(int id, int serviceType)
        {
            StopService(id, serviceType);
            StartService(id, serviceType);
        }

        public void UpService(int id, int serviceType, string password)
        {
            UnInstallService(id, serviceType);
            InstallService(id, serviceType, password);
        }

        #region private
        private void MoveDirectoryFile(string masterPath, string workPath)
        {
            if (!System.IO.Directory.Exists(workPath))
            {
                System.IO.Directory.CreateDirectory(workPath);
            }
            GlobalHelper.CopyFolder(masterPath, workPath, true);
        }

        private void BuildNewConfig(int serviceType,string[] args)
        {
            string filepath = GetWorkerPath(args,serviceType) + @"\" + GetMasterServiceConfigName(serviceType);
            ConverWriteFile(filepath, args);
            filepath = GetWorkerPath(args, serviceType) + @"\install.bat";
            ConverWriteFile(filepath, args);
            filepath = GetWorkerPath(args, serviceType) + @"\uninstall.bat";
            ConverWriteFile(filepath, args);
        }

        private void ConverWriteFile(string filepath, string[] args)
        {
            if (File.Exists(filepath))
            {
                string _content = string.Empty;
                System.IO.StreamReader reader = new StreamReader(filepath);
                _content = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();
                File.Delete(filepath);
                if (!File.Exists(filepath))
                {
                    _content = _content.Replace("${USERNAME}", args[0]);
                    _content = _content.Replace("${PASSWORD}", args[1]);
                    using (var fileSave = File.CreateText(filepath))
                    {
                        fileSave.Write(_content);
                        fileSave.Flush();
                    }
                }
            }
        }

        private string GetWorkerPath(string[] args, int serviceType)
        {
            EnumServiceType STYPE = (EnumServiceType)serviceType;
            string workPath = string.Empty;
            switch (STYPE)
            {
                case EnumServiceType.RC:
                    workPath = localSettings.CSWorkerDirectory + @"\" + args[0];
                    break;
                case EnumServiceType.RTWTS:
                    workPath = localSettings.RTWTSWorkerDirectory + @"\" + args[0];
                    break;
                default:
                    break;
            }
            return workPath;
        }

        /// <summary>
        /// 获取运行的服务名
        /// </summary>
        private string GetProcessServiceName(string[] args, int serviceType)
        {
            string serviceName = string.Empty;
            EnumServiceType STYPE = (EnumServiceType)serviceType;
            switch (STYPE)
            {
                case EnumServiceType.RC:
                    serviceName = "TipokHostManagerService-" + args[0];
                    break;
                case EnumServiceType.RTWTS:
                    serviceName = "TipokHostTeamService-" + args[0];
                    break;
                default:
                    break;
            }
            return serviceName;
        }

        /// <summary>
        /// 获取运行服务的配置文件名
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        private string GetMasterServiceConfigName(int serviceType)
        {
            string masterServiceConfigName = string.Empty;
            EnumServiceType STYPE = (EnumServiceType)serviceType;
            switch (STYPE)
            {
                case EnumServiceType.RC:
                    masterServiceConfigName = GlobalHelper.GetMasterConfigName(localSettings.CSMasertServiceName);
                    break;
                case EnumServiceType.RTWTS:
                    masterServiceConfigName = GlobalHelper.GetMasterConfigName(localSettings.RTWTSMasertServiceName);
                    break;
                default:
                    break;
            }
            return masterServiceConfigName;
        }
        #endregion 
    }
}
